/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef SHARED_DB_SQLCONNECTION_H_
#define SHARED_DB_SQLCONNECTION_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <cstdint>
#include <vector>
#include <string>
#include "Shared/Global_Macros.h"
#include "Shared/Base/Mutex.h"

namespace Shared {
namespace DB {
    class QueryResult;
    class SqlStmtParameters;
    class SqlPreparedStatement;
    class Database;
    

    class SqlConnection
    {
    public:
        SqlConnection(Database& db);
        virtual ~SqlConnection(void);

    public:
        virtual bool open(const std::string &host,
            const std::string &port, const std::string &user,
            const std::string &pass, const std::string &db, 
            bool autoReconnect = true) = 0;
        virtual void close(void) = 0;

        virtual QueryResult *query(const std::string &sql) = 0;
        virtual bool execute(const std::string &sql) = 0;
        
        virtual bool beginTransaction(void) = 0;
        virtual bool commitTransaction(void) = 0;
        virtual bool rollbackTransaction(void) = 0;

        virtual void ping(void) = 0;

        ///////////////////////////////////////////////////////////////////
        SqlPreparedStatement* createStatement(const std::string& fmt);
        SqlPreparedStatement* getStmt(std::uint32_t index);
        bool executeStmt(int index, const SqlStmtParameters& id);


        ///////////////////////////////////////////////////////////////////
        bool error(void) const { return _error; }
        int errorCode(void) const { return _errCode; }
        const std::string &errorString(void) const { return _errString; }
        Database& db(void) const { return _db; }

        ///////////////////////////////////////////////////////////////////
        class Lock
        {
        public:
            Lock(SqlConnection* conn) : _conn(conn) { _conn->_lock.lock(); }
            ~Lock() { _conn->_lock.unlock(); }
            SqlConnection* operator->() const { return _conn; }

        private:
            SqlConnection* const _conn;
        };

    protected:
        bool _error;
        int _errCode;
        ::std::string _errString;
        Database& _db;
        Mutex _lock;

        typedef std::vector<SqlPreparedStatement*> StmtHolder;
        StmtHolder _stmtHolder;

    private:
        DISALLOW_COPY_AND_ASSIGN(SqlConnection);
    };

}}

#endif // SHARED_DB_SQLCONNECTION_H_
